package com.auoem.radio.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
//import android.app.Fragment;

import com.auoem.radio.R;
import com.auctus.am.framework.enums.ApiStatusEnum;
import com.auoem.radio.common.custview.ProgressUtil;
import com.auoem.radio.common.utils.ToastUtils;

public abstract class BaseFragment extends Fragment implements View.OnClickListener {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        cancelProgress();
        super.onDestroyView();
    }

    private ProgressUtil mProgressUtil;

    public void showProgress(final String message) {
        if (mProgressUtil == null) {
            mProgressUtil = new ProgressUtil(getActivity());
        }
        mProgressUtil.showProgressDialog(message);
    }

    public void showProgress(final int message) {
        if (mProgressUtil == null) {
            mProgressUtil = new ProgressUtil(getActivity());
        }
        mProgressUtil.showProgressDialog(getActivity().getResources().getString(message));
    }

    public void cancelProgress() {
        if (mProgressUtil != null) {
            mProgressUtil.cancelProgressDialog();
        }
    }


    public void toastResult(ApiStatusEnum status) {
        cancelProgress();
        if (status == ApiStatusEnum.SUCCESS) {
            ToastUtils.showShortToast(getActivity(), R.string.am_common_exe_success);
        } else {
            ToastUtils.showShortToast(getActivity(), R.string.am_common_exe_failure);
        }
    }

    public void toastResultUI(final ApiStatusEnum status) {
        try {
            requireActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    cancelProgress();
                    if (status == ApiStatusEnum.SUCCESS) {
                        ToastUtils.showShortToast(getActivity(), R.string.am_common_exe_success);
                    } else {
                        ToastUtils.showShortToast(getActivity(), R.string.am_common_exe_failure);
                    }
                }
            });
        } catch (IllegalStateException e) {
            System.out.println("requireActivity illegal");
        }
    }

    public void toastResultUI(String msg) {
        try {
            requireActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    cancelProgress();
                    ToastUtils.showShortToast(getActivity(), msg);
                }
            });
        } catch (IllegalStateException e) {
            System.out.println("requireActivity illegal");
        }
    }

    public void toastResultUI(int msg) {
        try {
            requireActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    cancelProgress();
                    ToastUtils.showShortToast(getActivity(), msg);
                }
            });
        } catch (IllegalStateException e) {
            System.out.println("requireActivity illegal");
        }
    }

}
